package com.koron.css2.systemManage.servlet;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.dto.RoleUserDto;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class DeleteRoleUser implements ServerInterface{

	private static Logger log = LogManager.getLogger(DeleteRoleUser.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<String> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "添加成功", String.class);
		try {
			RoleUserDto roleUser = JsonUtils.objectToPojo(req.getData(), RoleUserDto.class);
			if(StringUtils.isEmpty(roleUser.getRoleCode())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("角色不能为空");
				return msg;
			}
			if(roleUser.getAccount() == null || roleUser.getAccount().size() == 0) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("账号不能为空");
				return msg;
			}
			OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
			OrgUserQueryBean query = new OrgUserQueryBean();
			query.setUserAccounts(roleUser.getAccount());
			List<OrgUserBean> orgUsers = orgUserMapper.selectByAccounts(query);
			ServiceFactory.getRoleService().deleteRoleAndUser(roleUser.getRoleCode(), orgUsers);
		} catch (Exception e) {
			log.error("删除角色用户出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("删除角色用户出错");
		}
		return msg;
	}

}
